package com.example.presentshopping.ui.activity

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.config.bean.SpellgroupBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.ui.custom.RoundImageView
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import kotlinx.android.synthetic.main.activity_experience.*
import kotlinx.android.synthetic.main.activity_supplier.*
import kotlinx.android.synthetic.main.activity_supplier.recycler_view
import kotlinx.android.synthetic.main.activity_supplier.refresh_layout
import kotlinx.android.synthetic.main.activity_supplier.title_main_tv

//商家商品列表
class SupplierActivity : BaseMvpActivity() {
    var spellgroupBran: SpellgroupBean? = null
    var baseRecyclerAdapter: BaseRecyclerAdapter<SpellgroupBean.DataBean.ListBean>? = null

    var id :  String ?=null
    var name :String ?=null
    var page =1

    private var queryInfoPresenter: PublicPresenter? = null
    override val layoutId: Int
        get() = R.layout.activity_supplier

    override fun initView(savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(context, false, "")
        queryInfoPresenter?.attachView(this)
        id =intent.getStringExtra("id")
        name =intent.getStringExtra("name")
        title_main_tv.leftBackImageTv.setOnClickListener { finish() }
        title_main_tv.titleTv.text =name
        setAdapter()
        refresh_layout.setOnRefreshListener {
            page = 1
            Goodslist(page)  }
        recycler_view?.setLoadMoreListener {
            page++
            Goodslist(page)
        }

        Goodslist(page)

    }
    fun  Goodslist(pata:Int){
        queryInfoPresenter?.netWorkRequestPost(HttpURL.Req_getSupplierGoodsList ,HttpRequest.getSupplierGoodsList( id,"",pata,10))
    }

    fun setAdapter(){
        // 使用默认的加载更多的View。
        recycler_view.useDefaultLoadMore()
        recycler_view.loadMoreFinish(false, true)
        baseRecyclerAdapter =object :BaseRecyclerAdapter<SpellgroupBean.DataBean.ListBean>(this,null,R.layout.item_discover){
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: SpellgroupBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val ivimage = holder?.getView<RoundImageView>(R.id.iv_image)
                val tv_name =holder?.getView<TextView>(R.id.tv_name)
                val tv_description =holder?.getView<TextView>(R.id.tv_description)
                val myodtunbsws =holder?.getView<LinearLayout>(R.id.myodtunbsws)

                tv_name?.text =data.title
                tv_description?.text = "¥: "+data.marketprice

                GlideUtil.load(context,data.thumb,ivimage)
                myodtunbsws?.setOnClickListener {
                    val Init = Intent(context, DetailsActivity().javaClass)
                    Init.putExtra("msid",data.id.toString())
                    Init.putExtra("Activity","ExperienceActivity")
                    context?.startActivity(Init)

                }

            }

        }
        recycler_view.adapter =baseRecyclerAdapter
    }


    override fun onDestroy() {
        queryInfoPresenter?.detachView()
        super.onDestroy()
    }

    override fun showToast(msg: String?) {
     ToastUtil.showMsg(msg)
    }

    override fun onSuccess(data: String?) {
        refresh_layout?.isRefreshing = false

        spellgroupBran = GsonUtils.getGson().fromJson(data, SpellgroupBean().javaClass)
        if (page==1){
            baseRecyclerAdapter?.clear()
        }
        if(spellgroupBran?.data?.list?.size!! >0){
            recycler_view.loadMoreFinish(false, true)
            baseRecyclerAdapter?.setData(spellgroupBran?.data?.list)
        }else{
            recycler_view.loadMoreFinish(true, false)
        }

    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }
}